﻿using GalaSoft.MvvmLight;

using System.Collections.ObjectModel;
using NtpChat.Client.Model;
using GalaSoft.MvvmLight.Command;

namespace NtpChat.Client.ViewModel
{

    public class ConversationViewModel : ViewModelBase
    {
        UserViewModel buddy;
        UserViewModel currentUser;
        ObservableCollection<MessageViewModel> messages;
        IService service;

        bool isActive;

        public ConversationViewModel()
        {
            Messages = new ObservableCollection<MessageViewModel>();
        }

        public ConversationViewModel(IService service, UserViewModel currentUser, UserViewModel buddy)
        {
            Messages = new ObservableCollection<MessageViewModel>();
            this.buddy = buddy;
            this.currentUser = currentUser;
            this.service = service;
        }

        public const string MessagePropertyName = "Messages";

        public ObservableCollection<MessageViewModel> Messages
        {
            get
            {
                return messages;
            }

            set
            {
                if (messages == value)
                {
                    return;
                }

                var oldValue = messages;
                messages = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(MessagePropertyName);

            }
        }

        /// <summary>
        /// The <see cref="MessageText" /> property's name.
        /// </summary>
        public const string MessageTextPropertyName = "MessageText";

        private string messageText = "";

        /// <summary>
        /// Gets the MessageText property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string MessageText
        {
            get
            {
                return messageText;
            }

            set
            {
                if (messageText == value)
                {
                    return;
                }

                var oldValue = messageText;
                messageText = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(MessageTextPropertyName);
            }
        }


        public UserViewModel Buddy
        {
            get
            {
                return buddy;
            }

           
        }

        public UserViewModel CurrentUser
        {
            get
            {
                return currentUser;
            }

           
        }

        public const string IsActivePropertyName = "IsActive";
        public bool IsActive
        {
            get
            {
                return isActive;
            }

            set
            {
                if (isActive == value)
                {
                    return;
                }

                var oldValue = isActive;
                isActive = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(IsActivePropertyName);
            }
        }

        private RelayCommand<string> sendMessageCommand;

        public RelayCommand<string> SendMessageCommand
        {
            get
            {
                if (sendMessageCommand == null)
                    sendMessageCommand = new RelayCommand<string>(AddMessage, CanAddMessage);
                return sendMessageCommand;
            }
        }

        public void AddMessage(string content)
        {
            this.AddMessage(new Message(currentUser.User,buddy.User, content));
        }


        public bool CanAddMessage(string content)
        {
            if (content == null)
                return false;
            return content.Length != 0;
        }
        private void AddMessage(Message message)
        {
            this.Messages.Add(new MessageViewModel(message));
            MessageText = "";
            SendMessage(message);
        }

        private void SendMessage(Message message)
        {
            service.SendMessage();
        }

    }
}